package com.example.service;

import com.example.dao.UserMapper;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service

public class UserServiceImpl implements UserService {
    UserMapper userMapper;
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /*
   user login
    */
    @Autowired(required = false)
    public User login(String id , String password) {
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        //创建user，调用login。进行user表的查找
        return userMapper.login(user);
    }

    /*
    user 注册
     */
    public String addUser(String id , String name, String password) {
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        user.setName(name);
        //先检查,有无重复的用户名（已注销的用户也算）
        if(userMapper.selectUser(user) != null) {
            return "注册失败，用户名重复";
        }
        else {
            if (userMapper.addUser(user) == 1){
                return "注册完成";
            }else {
                return "注册失败";
            }

        }

    }
    /*
    user 修改密码
     */
    @Autowired(required = false)
    public String changePassword(String id ,String password) {
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        if(userMapper.changePassword(user) == 1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    /*
    user 注销
     */
    public String deleteUser(String id) {
        User user = new User();
        user.setId(id);
        userMapper.deleteUser(user);
        return "已注销用户ID："+id;
    }
}
